//
//  Scene_Loading.m
//  StarBugs
//
//  Created by ?? ? on 6/26/12.
//  Copyright 2012 antisword@playorca.com. All rights reserved.
//

#include "Scene/Scene_Loading.h"
#include "Scene/Scene_MainMenu.h"
#include "Util/SoundManager.h"
#include "Util/ImageManager.h"
#include "Util/DataBase.h"
#include "Util/ModuleController.h"
#include "Global.h"

using namespace cocos2d;

CCScene* Scene_Loading::scene()
{
//    // 'scene' is an autorelease object
//    
//    CCScene *scene = CCScene::create();
//    
//    // add layer as a child to scene
//    CCLayer* layer = new Scene_Loading();
//    scene->addChild(layer);
//    layer->release();
//    
//
//    
//    return scene;
	CCLog("pwj --- Scene_Loading::scene()");
    CCLayer* layer = new Scene_Loading();
    layer->autorelease();
    return (CCScene *)layer;
}

Scene_Loading::~Scene_Loading()
{
//     [Texture2D setDefaultAlphaPixelFormat:kTexture2DPixelFormat_RGBA4444];
//    CCTexture2D::setDefaultAlphaPixelFormat(kTexture2DPixelFormat_RGBA4444);
    ImageManager::closeXml(kImageSheet_loading);
    SoundManager::PlaySoundBgm(bgm_1);
    CCTextureCache::sharedTextureCache()->removeUnusedTextures();

}

Scene_Loading::Scene_Loading()
{

    ModuleController::startStore();
    CCTextureCache::sharedTextureCache()->removeUnusedTextures();
    ImageManager::openXml(kImageSheet_loading);
    CCSize size = CCDirector::sharedDirector()->getWinSize();

#ifdef __ANDROID_
    setKeypadEnabled(true);
#endif
    SoundManager::initSoundEffect();
    SoundManager::muteSoundBgm(Database::getInstancePtr()->gameDatabase.isBgSound);
    SoundManager::muteSoundEffect(Database::getInstancePtr()->gameDatabase.isEffSound);
    
    CCSprite *sprite;
    CCDelayTime *delay;
    CCCallFunc *call;
    
    CCTexture2D::setDefaultAlphaPixelFormat(kCCTexture2DPixelFormat_RGB565);
    screenLayer[0] = CCLayer::create();
    addChild(screenLayer[0]);
    screenLayer[1] = CCLayer::create();
    addChild(screenLayer[1]);
    if(ModuleController::getIs5())
        sprite = CCSprite::create("Default-568h@2x.png");
    else if(ModuleController::getIsIpad())
        sprite = CCSprite::create("Default-Portrait.png");
    else if(CC_CONTENT_SCALE_FACTOR() == 2) 
        sprite = CCSprite::create("Default@2x.png");
    else
        sprite = CCSprite::create("Default.png");

    
    sprite->setAnchorPoint(ccp(0.5, 0.5));
    sprite->setPosition(ccp(size.width/2, size.height/2));
    screenLayer[0]->addChild(sprite);
    CCTexture2D::setDefaultAlphaPixelFormat(kCCTexture2DPixelFormat_Default);
    

    screenLayer[1]->setContentSize(CCSize(320 * ModuleController::getImageScale(), 480 * ModuleController::getImageScale()));
    screenLayer[1]->setPosition(ccp(size.width/2 - screenLayer[1]->getContentSize().width/2, size.height/2 - screenLayer[1]->getContentSize().height/2));
    screenLayer[1]->setScale(ModuleController::getScaleFactor_w());
    
    
//    screenLayer[1]->setAnchorPoint(ccp(0.5, 0.5));
//    screenLayer[1]->setPosition(ccp(size.width/2, size.height/2));
//    screenLayer[1]->setScale(ModuleController::getScaleFactor());
    
    sprite = ImageManager::getSpriteWithXml2(kImageSheet_loading, "background");
    sprite->setPosition(ccp(160, 240));
    sprite->setScaleY(ModuleController::getSceneH() / 480);
    screenLayer[1]->addChild(sprite);
    
    
    sprite = ImageManager::getSpriteWithXml2(kImageSheet_loading, "orca");
    sprite->setPosition(ccp(160, 240));
    screenLayer[1]->addChild(sprite);

    
    starSprite[0] = ImageManager::getSpriteWithXml2(kImageSheet_loading, "star1");
    starSprite[0]->setPosition(ccp(72, 222));
    starSprite[0]->setAnchorPoint(ccp(1, 0));
    starSprite[0]->setScale(0);
    screenLayer[1]->addChild(starSprite[0]);
    
    starSprite[1] = ImageManager::getSpriteWithXml2(kImageSheet_loading, "star2");
    starSprite[1]->setPosition(ccp(67, 218));
    starSprite[1]->setAnchorPoint(ccp(1, 0));
    starSprite[1]->setScale(0);
    screenLayer[1]->addChild(starSprite[1]);
    
    starSprite[2] = ImageManager::getSpriteWithXml2(kImageSheet_loading, "star3");
    starSprite[2]->setPosition(ccp(243, 216));
    starSprite[2]->setAnchorPoint(ccp(0, 0));
    starSprite[2]->setScale(0);
    screenLayer[1]->addChild(starSprite[2]);
    
    starSprite[3] = ImageManager::getSpriteWithXml2(kImageSheet_loading, "star4");
    starSprite[3]->setPosition(ccp(253, 213));
    starSprite[3]->setAnchorPoint(ccp(0, 0));
    starSprite[3]->setScale(0);
    screenLayer[1]->addChild(starSprite[3]);
    
    screenLayer[1]->setVisible(false);
#ifdef __IOS_
    delay = CCDelayTime::create(2);
    call = CCCallFunc::create(this, callfunc_selector(Scene_Loading::call_layerchange));
    runAction(CCSequence::create(delay, call, NULL));
#endif
#ifdef __ANDROID_
    call_layerchange();
#endif

}

void Scene_Loading::start()
{
    CCDirector::sharedDirector()->replaceScene(Scene_MainMenu::scene());
}

void Scene_Loading::call_layerchange()
{
    screenLayer[0]->setVisible(false);
    screenLayer[1]->setVisible(true);
    
    CCDelayTime *delay = CCDelayTime::create(3);
    CCCallFunc *call = CCCallFunc::create(this, callfunc_selector(Scene_Loading::start));
    runAction(CCSequence::create(delay, call, NULL));
    
    CCScaleTo *scale1, *scale2, *scale3;
    
    scale1 = CCScaleTo::create(0.2, 1.2);
    scale2 = CCScaleTo::create(0.1, 0.8);
    scale3 = CCScaleTo::create(0.1, 1.0);
    starSprite[0]->runAction(CCSequence::create(scale1, scale2, scale3, NULL));
    
    delay = CCDelayTime::create(0.15);
    scale1 = CCScaleTo::create(0.2, 1.2);
    scale2 = CCScaleTo::create(0.1, 0.8);
    scale3 = CCScaleTo::create(0.1, 1.0);
    starSprite[1]->runAction(CCSequence::create(delay, scale1, scale2, scale3, NULL));
    
    delay = CCDelayTime::create(0.3);
    scale1 = CCScaleTo::create(0.2, 1.2);
    scale2 = CCScaleTo::create(0.1, 0.8);
    scale3 = CCScaleTo::create(0.1, 1.0);
    starSprite[2]->runAction(CCSequence::create(delay, scale1, scale2, scale3, NULL));
    
    delay = CCDelayTime::create(0.45);
    scale1 = CCScaleTo::create(0.2, 1.2);
    scale2 = CCScaleTo::create(0.1, 0.8);
    scale3 = CCScaleTo::create(0.1, 1.0);
    starSprite[3]->runAction(CCSequence::create(delay, scale1, scale2, scale3, NULL));
        
    ModuleController::authenticateLocalUser();
}
void Scene_Loading::keyBackClicked()
{
#ifdef __ANDROID_
    CCDirector::sharedDirector()->end();
#endif
}